package com.jshcbd.mvp_kt_demo.view

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration

class VerticalSpacingItemDecoration : ItemDecoration {
    private var mSpace: Int
    private var mEdgeSpace = 0

    constructor(mSpace: Int) {
        this.mSpace = mSpace
    }

    constructor(mSpace: Int, EdgeSpace: Int) {
        this.mSpace = mSpace
        mEdgeSpace = EdgeSpace
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        //简单写法--仅仅判断了第一个
        val position = parent.getChildAdapterPosition(view) // item position
        if (position != 0) {
            outRect[0, mSpace, 0] = 0
        }

//        RecyclerView.LayoutManager manager = parent.getLayoutManager();
//        int childPosition = parent.getChildAdapterPosition(view);
//        int itemCount = parent.getAdapter().getItemCount();
//        if (manager != null) {
//            if (manager instanceof GridLayoutManager) {
//                setGridOffset(
//                    ((GridLayoutManager) manager).getOrientation(),
//                    ((GridLayoutManager) manager).getSpanCount(),
//                    outRect,
//                    childPosition,
//                    itemCount
//                );
//            } else if (manager instanceof LinearLayoutManager) {
//                setLinearOffset(
//                    ((LinearLayoutManager) manager).getOrientation(),
//                    outRect,
//                    childPosition,
//                    itemCount
//                );
//            }
//        }
    }

    /**
     * 设置LinearLayoutManager 类型的 offset
     *
     * @param orientation   方向
     * @param outRect       padding
     * @param childPosition 在 list 中的 position
     * @param itemCount     list size
     */
    private fun setLinearOffset(
        orientation: Int,
        outRect: Rect,
        childPosition: Int,
        itemCount: Int
    ) {
        if (orientation == LinearLayoutManager.HORIZONTAL) {
            if (childPosition == 0) {
                // 第一个要设置PaddingLeft
                outRect[mEdgeSpace, 0, mSpace] = 0
            } else if (childPosition == itemCount - 1) {
                // 最后一个设置PaddingRight
                outRect[0, 0, mEdgeSpace] = 0
            } else {
                outRect[0, 0, mSpace] = 0
            }
        } else {
            when (childPosition) {
                0 -> {
                    // 第一个要设置PaddingTop
                    outRect[0, mEdgeSpace, 0] = mSpace
                }
                itemCount - 1 -> {
                    // 最后一个要设置PaddingBottom
                    outRect[0, 0, 0] = mEdgeSpace
                }
                else -> {
                    outRect[0, 0, 0] = mSpace
                }
            }
        }
    }

    /**
     * 设置GridLayoutManager 类型的 offset
     *
     * @param orientation   方向
     * @param spanCount     个数
     * @param outRect       padding
     * @param childPosition 在 list 中的 position
     * @param itemCount     list size
     */
    private fun setGridOffset(
        orientation: Int,
        spanCount: Int,
        outRect: Rect,
        childPosition: Int,
        itemCount: Int
    ) {
        val totalSpace = (mSpace * (spanCount - 1) + mEdgeSpace * 2).toFloat()
        val eachSpace = totalSpace / spanCount
        val column = childPosition % spanCount
        val row = childPosition / spanCount
        var left: Float
        var right: Float
        var top: Float
        var bottom: Float
        if (orientation == GridLayoutManager.VERTICAL) {
            top = 0f
            bottom = mSpace.toFloat()
            if (childPosition < spanCount) {
                top = mEdgeSpace.toFloat()
            }
            bottom = getBottom(spanCount, itemCount, row, bottom)
            if (spanCount == 1) {
                left = mEdgeSpace.toFloat()
                right = left
            } else {
                left = column * (eachSpace - mEdgeSpace - mEdgeSpace) / (spanCount - 1) + mEdgeSpace
                right = eachSpace - left
            }
        } else {
            left = 0f
            right = mSpace.toFloat()
            if (childPosition < spanCount) {
                left = mEdgeSpace.toFloat()
            }
            right = getRight(spanCount, itemCount, row, right)
            if (spanCount == 1) {
                top = mEdgeSpace.toFloat()
                bottom = top
            } else {
                top = column * (eachSpace - mEdgeSpace - mEdgeSpace) / (spanCount - 1) + mEdgeSpace
                bottom = eachSpace - top
            }
        }
        outRect[left.toInt(), top.toInt(), right.toInt()] = bottom.toInt()
    }

    private fun getBottom(spanCount: Int, itemCount: Int, row: Int, bottom: Float): Float {
        var bottom = bottom
        if (itemCount % spanCount != 0 && itemCount / spanCount == row ||
            itemCount % spanCount == 0 && itemCount / spanCount == row + 1
        ) bottom = mEdgeSpace.toFloat()
        return bottom
    }

    private fun getRight(spanCount: Int, itemCount: Int, row: Int, right: Float): Float {
        var right = right
        if (itemCount % spanCount != 0 && itemCount / spanCount == row ||
            itemCount % spanCount == 0 && itemCount / spanCount == row + 1
        ) right = mEdgeSpace.toFloat()
        return right
    }
}